import { sqlMain } from '@utils/dbconf'
import { cpttmap,usermap } from '@maps/index'
import { success,error,SQL_ERROR } from '@utils/response'
import { dateFormat } from '@utils/functions'
import { tokenTo } from '@utils/tokenOper'
import { analysis } from '@utils/request'
import redisDB from '@utils/redisconf'
import fs from 'fs'
const ADVER_PATH = 'files/adver/'
const CPTT_IMG_PATH = 'file/cpttimg/'

export const select = async (req,res) =>{
  try{
    const { params } = res.locals
    const { state } = params
    console.log({params,state})
    if(!state || !/^[1-4]$/.test(state)) return res.send(error(500,'参数异常'))
    const dict = {
      '1':'select_all',
      '2':'select_no_over_home',
      '3':'select_no_going_home',
      '4':'select_delete',
    }
    const cptts = await sqlMain(cpttmap[dict[state]],[])
    for(const item of cptts){
      const { imgname,overtime,goingtime,uid } = item
      if(imgname)
        item.imgname = `${CPTT_IMG_PATH}${imgname}`
      // delete item.imgname
      item.overtime = dateFormat('YYYY-MM-DD hh:mm:ss',overtime)
      item.goingtime = dateFormat('YYYY-MM-DD hh:mm:ss',goingtime)
      item.state = state
      let organizer
      organizer = await redisDB.get(3,uid)
      if(!organizer){
        sqlMain(usermap.selectname,[uid],true)
        organizer = await sqlMain(usermap.selectname,[uid],true).then(e=>e.name)
        await redisDB.set(3,uid,organizer,10)
      }
      if(item.A) delete item.A
      item.organizer = organizer
    }
    // setTimeout(()=>{
      return res.send(success({cptts}))
    // },5000)
  }catch(e){
    return res.send(SQL_ERROR)}
}

export const event = async (req,res) => {
  try{
    const { id } = res.locals.params
    const cptt = await sqlMain(cpttmap.selectone,[id],true)
    return res.send(success({cptt}))
  }catch(e){
    return res.send(SQL_ERROR)}
}

export const getapplay = async (req,res) =>{
  try{
    const { id } = res.locals.params
    const applays = await sqlMain(cpttmap.getapplay,[id])
    if(applays.length)
      applays.forEach(e=>{
        delete e.cptt_id
        delete e.user_id
      })
    return res.send(success({applays}))
  }catch(e){
    return res.send(SQL_ERROR)}
}

export const getmyapplay = async (req,res) =>{
  try{
    const { id } = await tokenTo(res.locals.token)
    const applays = await sqlMain(cpttmap.getmyapplay,[id])
    // 
    return res.send(success({applays}))
  }catch(e){
    return res.send(SQL_ERROR)}
}

export const applaycptt = async (req,res) => {
  try{
    const { data,token } = res.locals
    const { cptype,cpid } = data
    const { id:uid } = await tokenTo(token)
    let affectedRows
    if(cptype){//组队比赛
      const { make,keycode } = data
      if(make)//自己创建
        affectedRows = await sqlMain(cpttmap.joinAteam,[cpid,uid,keycode,1,cpid,uid]).then(e=>e.affectedRows)
      else{
        const { num } = await sqlMain(cpttmap.canjoin,[cpid,keycode],true)
        if(num==0)
          return res.send(error(500,'队伍未找到,您可以创建一个队伍或检查识别码'))
        else if(num==1 || num==2)
          affectedRows = await sqlMain(cpttmap.joinAteam,[cpid,uid,keycode,0,cpid,uid]).then(e=>e.affectedRows)
        else
          return res.send(error(500,'队伍已满'))
      }
    }else{//个人比赛
      affectedRows = await sqlMain(cpttmap.applay,[cpid,uid,cpid,uid]).then(e=>e.affectedRows)
    }
    return res.send(affectedRows?success():error(500,'您已报名该比赛,不可重复报名'))
  }catch(e){
    console.log(e)
    return res.send(SQL_ERROR)}
}

export const getmygroup = async (req,res) => {
try{
  const { params,token } = res.locals
  const { id:cpid } = params
  console.log({token})
  const { id:uid } = await tokenTo(token)
  const { KEYCODE } = await sqlMain(cpttmap.getkeycode,[uid,cpid],true)
  const groups = await sqlMain(cpttmap.getgroupbycode,[KEYCODE,cpid])
  const { length } = groups
  console.log({length,groups})
  if(length){
    const avas = await sqlMain(cpttmap.selectavabyid([groups.map(e=>e.user_id)]),[])
    groups.forEach(item=>{
      const info = avas.find(e=>item.user_id == e.u_id)
      console.log({name:info.name})
      item.ava = info.ava
      item.name = info.name
    })
  }
  return res.send(success({groups}))
}catch(e){
    console.log(e)
    return res.send(SQL_ERROR)}
}

export const getisapplay = async (req,res) => {
try{
  const { params } = res.locals
  const { cpid,keycode } = params
  // const { id:uid } = await tokenTo(token)
  console.log({cpid,keycode})
  const groups = await sqlMain(cpttmap.getgroupbycode,[keycode,cpid])
  const { length } = groups
  if(length === 3 ) res.send(error(500,'人数已满'))
  if(length){
    const avas = await sqlMain(cpttmap.selectavabyid([groups.map(e=>e.user_id)]),[])
    groups.forEach(item=>{
      const info = avas.find(e=>item.user_id == e.u_id)
      console.log({name:info.name})
      item.ava = info.ava
      item.name = info.name
    })
  }
  return res.send(success({groups}))
}catch(e){
    console.log(e)
    return res.send(SQL_ERROR)}
}